package ModelLayer;

import java.util.ArrayList;

/**
 * Write a description of class DVDCollection here.
 * 
 * @author (Group1:Georgi,Roland and Mindaugas) 
 * @version (a version number or a date)
 */
public class DVDCollection
{
    private ArrayList<DVD> DVDs;
    private static DVDCollection collection;

    /**
     * Constructor for objects of class DVDCollection
     */
    public DVDCollection()
    {
       DVDs=new ArrayList<DVD>();

    }
    
    /**
     * Return an existing collection or if there is no collection it is going to create it.
     */
    public static  DVDCollection getDVDCollection()
    {
        if(collection==null)
          collection=new DVDCollection ();
          
        return  collection;
    }
    
    /**
     * Add's a DVD to the collection.
     */
    public void addDVD(DVD dvd)
    {
        DVDs.add(dvd);
    }
    
    /**
     * Searches for a DVD in the collection using its ID.
     */
    public DVD searchDVD(long ID)
    {
       for(int i = 0; i<DVDs.size(); i++){
           
           if(DVDs.get(i).getID()==ID){
               return DVDs.get(i);
            }
           
        }        
        return null;
    }
    
    /**
     * Checks if a DVD exists using its ID.
     */
    public boolean dvdExist(long ID)
    {
      if(searchDVD(ID)==null)
        return false;
     
      else
         return true;
    }
    
    /**
     * Checks if a copy of a certain Dvd exists using the DVD's id and copyID.
     */
    public boolean copyExist(long dvdID,long copyID)
    {
       if(dvdExist(dvdID))
          if(searchDVD(dvdID).copyExist(copyID))
             return true;
             
       return false;      
    }
    
    /**
     * Creates a new DVD.
     */
    public void createDVD(long ID, String title, String artist, String publicationDate)
    {
        if(searchDVD(ID)==null)
        {
         DVD dvd = new DVD(ID, title, artist, publicationDate);
         addDVD(dvd);
        }
        
        else
          throw new RuntimeException();
                 
    }
    
    /**
     * Deletes an existing DVD using its ID.
     */
    public void deleteDVD(long ID)
    {
        DVDs.remove(searchDVD(ID));
    }
    
    /**
     * Returns all the titles of the DVDs.
     */
    public ArrayList<String> getAllTitles()
    {
       ArrayList<String> titles=new ArrayList<String>();
    
       for(int i=0;i<DVDs.size();i++)
         titles.add(DVDs.get(i).getTitle());
         
       return titles;  
    }
    
    /**
     * Returns all the ID's of the DVDs.
     */
    public ArrayList<Long> getAllIDs()
    {
       ArrayList<Long> ids=new ArrayList<Long>();
    
       for(int i=0;i<DVDs.size();i++)
         ids.add(DVDs.get(i).getID());
         
       return ids;  
    }
    

    /**
     * Returns all the Artists of the DVDs.
     */
     public ArrayList<String> getAllArtists()
    {
       ArrayList<String> artists=new ArrayList<String>();
    
       for(int i=0;i<DVDs.size();i++)
         artists.add(DVDs.get(i).getArtist());
         
       return artists;  
    }
    
    /**
     * Returns all the Publication Dates of the DVDs.
     */
    public ArrayList<String> getPublicationDates()
    {
       ArrayList<String> dates=new ArrayList<String>();
    
       for(int i=0;i<DVDs.size();i++)
         dates.add(DVDs.get(i).getPublicationDate());
         
       return dates;  
    }
        
    
}
